// @ts-nocheck
import { useState, useEffect } from "react";
import { signIn } from "next-auth/react";
import { Spinner } from "@/src/components/layouts/spinner";

export default function SignIn() {
  useEffect(() => {
    console.log("SignIn");
    onCredentialsSubmit({ email: 'lei.peng@yisquare.com.cn', password: '123456@abc', projectId: 'cm9zdhn7i0006p407v4pxbaar' });
    const handleMessage = (event) => {
      if (event.data?.email && event.data?.password) {
        onCredentialsSubmit({ email: event.data?.email, password: event.data?.password, projectId: event.data?.projectId });
      } else {
        onCredentialsSubmit({ email: 'lei.peng@yisquare.com.cn', password: '123456@abc', projectId: 'cm9zdhn7i0006p407v4pxbaar' });
      }
    };
    window.addEventListener('message', handleMessage);
  }, []);
  /**
   * @param {{ email: any; password: any; }} values
   */
  async function onCredentialsSubmit(values) {
    try {
      const result = await signIn("credentials", {
        email: values.email,
        password: values.password,
        callbackUrl: `/project/${values.projectId}/traces`,
        redirect: false,
      });
      if (result.ok) {
        window.location.href = result.url;
      }
    } catch (error) {
      console.error(error);
    }
  }

  return (
    <>
      <Spinner message="Loading" />
    </>
  );
}
